$(function () {
    //发起ajax请求获取用户详细信息
    $.ajax({
        url: Bignews.user_detail,
        headers: headers,
        success: function (res) {
            console.log(res);
            //获取数据后填充到页面
            $('#inputEmail1').val(res.data.username)
            $('#inputEmail2').val(res.data.nickname)
            $('#inputEmail3').val(res.data.email)
            $('.user_pic').attr('src', res.data.userPic)
            $('#inputEmail4').val(res.data.password)
        }
    })

    //给修改头像的选择文件按钮添加change事件,实现图片预览
    $('#exampleInputFile').on('change', function () {
        let userPic = $(this)[0].files[0]
        if (userPic.type.indexOf('image/') === -1) {
            alert('请上传图片文件')
            console.log(userPic);
            return
        }
        $('.user_pic').attr('src',URL.createObjectURL(userPic))
    })

    //给修改按钮添加点击事件,收集页面的信息,发起修改信息的ajax请求
    $('.btn-edit').on('click', function () {
        console.log(1209);
        //创建一个formdata实例
        let formdata = new FormData()
        //收集页面信息,包括用户名称,昵称,邮箱,图片地址,用户密码
        let username = $('#inputEmail1').val()
        let nickname = $('#inputEmail2').val()
        let email = $('#inputEmail3').val()
        let password = $('#inputEmail4').val()
        let userPic = $('#exampleInputFile')[0].files[0]

        console.log(username,nickname,email,password,userPic);
        formdata.append('userPic',userPic)
        formdata.append('username',username)
        formdata.append('nickname',nickname)
        formdata.append('email',email)
        formdata.append('password',password)
        
        //发起ajax请求,修改用户信息
        $.ajax({
            type: 'post',
            url: Bignews.user_edit,
            data: formdata,
            contentType: false,
            processData: false,
            headers: headers,
            dataType: 'json',
            success: function (res) {
                console.log(res);
                alert(res.msg)
                top.location.href = "./index.html"
            }
        })
    })
})